#include "aeswindow.h"
#include "ui_aeswindow.h"

#include <QMessageBox>

#define AES_128 0
#define AES_192 1
#define AES_256 2


int key_len = AES_NULL;

aeswindow::aeswindow(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::aeswindow)
{
    ui->setupUi(this);
    /// 窗口在Close()后自动释放内存
    this->setAttribute(Qt::WA_DeleteOnClose);
}

aeswindow::~aeswindow()
{
    emit send_aes_config_information(NULL,0,0,0);
    delete ui;
}

void aeswindow::close_aes_window()
{
    this->close();
}

void aeswindow::on_aes_ok_button_clicked()
{
    if(key_len == AES_NULL)
    {
        QMessageBox::warning(this,"警告","请注意密钥长度只能是128，192，256");
        return;
    }

    emit send_aes_config_information(ui->keys_line->text(),
                                     key_len,
                                     ui->mod_combox->currentIndex(),
                                     ui->padding_combox->currentIndex());
    this->close();
}


void aeswindow::on_keys_line_editingFinished()
{
    /// qDebug()<<ui->keys_line->text().toInt();
    if(ui->keys_line->text().isEmpty())
    {
        QMessageBox::warning(this,"警告","请输入密钥");
    }
    else if(ui->keys_line->text().length()*8 == 128)
    {
        ui->key_len_label->setText("128");
        key_len = AES_128;
    }
    else if(ui->keys_line->text().length()*8 == 192)
    {
        ui->key_len_label->setText("192");
        key_len = AES_192;
    }
    else if(ui->keys_line->text().length()*8 == 256)
    {
        ui->key_len_label->setText("256");
        key_len = AES_256;
    }
    else
    {
        key_len = AES_NULL;
        ui->key_len_label->setText("非法数据长度");
        QMessageBox::warning(this,"警告","请注意密钥长度只能是128，192，256");
    }
}

